package com.jc.service.api;

import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.*;
import com.jc.framework.util.SpringUtils;
import com.jc.service.attribute.AttributeService;
import com.jc.service.attributevalue.AttributeValueService;
import com.jc.service.producttype.ProductTypeService;
import com.jc.service.pullproductlog.PullProductLogService;
import com.jc.service.supplierproduct.SupplierProductService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@SuppressWarnings("all")
public abstract class ProductCheckAdapter {

    ProductTypeService productTypeService = SpringUtils.getBean(ProductTypeService.class);
    SupplierProductService supplierProductService = SpringUtils.getBean(SupplierProductService.class);
    PullProductLogService pullProductLogService = SpringUtils.getBean(PullProductLogService.class);
    AttributeService attributeService = SpringUtils.getBean(AttributeService.class);
    AttributeValueService attributeValueService = SpringUtils.getBean(AttributeValueService.class);


    /**
     * 校验推送过来的产品是否符合要求 -- 类型
     * 类型名称,产品SKUId , 第三方SKU id
     * */
    public Boolean checkProType(String typeName , Long productId , Long pid){
        Boolean result = true;
        ProductType productType = new ProductType();
        productType.setTypeName(typeName);
        List<ProductType> productTypes = productTypeService.queryAll(productType);
        if (CollectionUtils.isEmpty(productTypes) || StringUtils.isEmpty(typeName)){
            //信息不全  分类不存在
            SupplierProduct upProduct = new SupplierProduct();
            upProduct.setId(productId);
            upProduct.setIsPerfect("N");
            supplierProductService.modify(upProduct,false);

            PullProductLog pullProductLog = new PullProductLog();
            pullProductLog.setProductId(productId);
            pullProductLog.setProductPid(pid);
            pullProductLog.setIsProductType("Y");
            pullProductLog.setProductTypeText(typeName);
            pullProductLogService.add(pullProductLog,false);
            return false;
        }
        return result;
    }


     /** 校验推送过来的产品是否符合要求 -- 属性
     * 属性名称,产品SKUId , 第三方SKU id
     * */
    public Boolean checkProAttribute(String attributeName , Long productId , Long pid){
        Boolean result = true;
        ProAttribute skuAttr = new ProAttribute();
        skuAttr.setProductAttributeName(attributeName);
        List<ProAttribute> attributes = attributeService.queryAll(skuAttr);
        if (CollectionUtils.isEmpty(attributes) || StringUtils.isEmpty(attributeName)){
            SupplierProduct upProduct = new SupplierProduct();
            upProduct.setId(productId);
            upProduct.setIsPerfect("N");
            supplierProductService.modify(upProduct,false);

            PullProductLog pullProductLog = new PullProductLog();
            pullProductLog.setProductId(productId);
            pullProductLog.setProductPid(pid);
            List<PullProductLog> pullProductLogs = pullProductLogService.queryAll(pullProductLog);
            pullProductLog.setIsProductAttribute("Y");
            if (CollectionUtils.isEmpty(pullProductLogs)){
                pullProductLog.setProductAttributeText(attributeName);
                pullProductLogService.add(pullProductLog,false);
            }else{
                if(StringUtils.isEmpty(pullProductLogs.get(0).getIsProductAttribute())){
                    pullProductLog.setProductAttributeText(attributeName+","+pullProductLogs.get(0).getIsProductAttribute());
                }else{
                    pullProductLog.setProductAttributeText(attributeName);
                }
                pullProductLog.setId(pullProductLogs.get(0).getId());
                pullProductLogService.modify(pullProductLog,false);
            }
            return false;
        }
        return result;
    }


     /** 校验推送过来的产品是否符合要求 -- 属性值
     * 属性名称,属性值名称,产品SKUId , 第三方SKU id
     * */
    public Boolean checkProAttributeValue(String attributeName , String attributeValueName , Long productId , Long pid){
        Boolean result = true;
        ProAttribute skuAttr = new ProAttribute();
        skuAttr.setProductAttributeName(attributeName);
        List<ProAttribute> attributes = attributeService.queryAll(skuAttr);
        if (CollectionUtils.isEmpty(attributes)){
            throw new BaseServiceException("属性为空");
        }
        AttributeValue attributeValue = new AttributeValue();
        attributeValue.setAttributeValue(attributeValueName);
        attributeValue.setAttributeNameId(attributes.get(0).getId());
        List<AttributeValue> attributeValues = attributeValueService.queryAll(attributeValue);
        if (CollectionUtils.isEmpty(attributeValues) || StringUtils.isEmpty(attributeValueName)) {
            //修改
            SupplierProduct upProduct = new SupplierProduct();
            upProduct.setId(productId);
            upProduct.setIsPerfect("N");
            supplierProductService.modify(upProduct, false);
            //属性值不存在
            PullProductLog pullProductLog = new PullProductLog();
            pullProductLog.setProductId(productId);
            pullProductLog.setProductPid(pid);
            List<PullProductLog> pullProductLogs = pullProductLogService.queryAll(pullProductLog);
            pullProductLog.setIsProductAttributeValue("Y");
            if (CollectionUtils.isEmpty(pullProductLogs)) {
                pullProductLog.setProductAttributeValueText(attributeValueName);
                pullProductLogService.add(pullProductLog, false);
            } else {
                if (StringUtils.isEmpty(pullProductLogs.get(0).getIsProductAttributeValue())) {
                    pullProductLog.setProductAttributeValueText(attributeValueName + "," + pullProductLogs.get(0).getProductAttributeValueText());
                } else {
                    pullProductLog.setProductAttributeValueText(attributeValueName);
                }
                pullProductLog.setId(pullProductLogs.get(0).getId());
                pullProductLogService.modify(pullProductLog, false);
            }
            return false;
        }
        return result;
    }
    
    
    public SupplierProductPrice getPrice(Long productId,int _salePrice,int _costPrice,int _defaultFreight) {
    	
    	
    	int freight = _defaultFreight;
		// 保存SKU价格信息，涉及到有计算
		int private_cost_price = _costPrice ;//成本价,因为现在是全场包邮,所以这里要全部加上邮费
		int salePrice = _salePrice + freight;; //市场价，因为现在是全场包邮,所以这里要全部加上邮费
	
		// 求差价
		int diffPrice = salePrice - private_cost_price;
		if (diffPrice < 0) {
			return null;
		}
    	int step = diffPrice / 4; // 四个等级，每级的差价
		SupplierProductPrice _supplierProductPrice = new SupplierProductPrice();
		_supplierProductPrice.setSupplierProductId(productId);
		_supplierProductPrice.setDefaultFreight(freight);
		_supplierProductPrice.setCooperateprice(_costPrice);
		_supplierProductPrice.setTaxcooperateprice(_costPrice);
		_supplierProductPrice.setSaleprice(salePrice);
		_supplierProductPrice.setMoneyPrice(salePrice);
		_supplierProductPrice.setLeve1Price(private_cost_price + step + step + step);
		_supplierProductPrice.setLeve2Price(private_cost_price + step + step);
		_supplierProductPrice.setLeve3Price(private_cost_price + step);
		_supplierProductPrice.setLeve4Price(private_cost_price);
		_supplierProductPrice.setCostPrice(private_cost_price);
		_supplierProductPrice.setUnionCostPrice(private_cost_price);
		return _supplierProductPrice;
    }
    
    
    
    
    
    
    
    
}
